package com.etc.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.SqlSessionFactoryUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

//    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Autowired
    private UserMapper mapper;




    @Override
    public List<User> selectUserByPrimaryKey(int id) {
        List<User> list = mapper.selectUserByPrimaryKey(id);
        return list;
    }

    @Override
    public List<User> select_All() {
        List<User> list = mapper.select_All();
        return list;
    }



    @Override
    public boolean update_UserByPrimaryKeySelective(User record) {
        int result = mapper.update_UserByPrimaryKeySelective(record);
        return result > 0;
    }

    @Override
    public boolean update_UserPassword(int id,String password) {
        String md5Pwd = SecureUtil.md5(password);
        int result = mapper.update_UserPassword(id,md5Pwd);
        return result > 0;
    }

    public List<User> select_UserLogin(@Param("phone") String phone, @Param("password") String password){
        String md5Pwd = SecureUtil.md5(password);
        List<User> list = mapper.select_UserLogin(phone, md5Pwd);
        return list;
    }

    @Override
    public boolean insert_User(User record) {
        User user1 = mapper.selectByUserphone(record.getUserphone());
        if (user1==null){
            return false;
        }
        String md5Pwd = SecureUtil.md5(record.getUserpwd());
        record.setUserpwd(md5Pwd);
        int result = mapper.insert_User(record);
        return result > 0;
    }
}
